﻿using System;
using System.Collections;
using System.Xml;
using System.Text;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	
	class FormBuildMaxLengthValueWorker
	{
		
		public static void buildMaxLengthValue(Form form)
		{
			XmlElement root = form.XmlDoc.DocumentElement;
			StringBuilder valueBuffer = new StringBuilder();
			ArrayList xmlToEditList = form.Manifest.XmlToEditList;
			foreach (XmlToEdit xmlToEdit in xmlToEditList)
			{
				//CID 0543 //2010.04.30 //if
				if( xmlToEdit.ViewName.Equals( form.View.Name ) )
				{
					if (xmlToEdit.Component.Equals(JetConstant.CMP_xField) && xmlToEdit.MaxLength > 0)
					{
						if( xmlToEdit.Type.Equals( JetConstant.xField_Rich ) && xmlToEdit.MaxLength == 1 )
						{
							//如果type是"rich",并且maxLength="1",实际上maxLength不起作用,见RichTextBox.xsn.
							continue;
						}
						XmlNodeList nodeList = DOMUtil.selectNodes(root, xmlToEdit.Item, 0, form.XmlNsResolver);
						foreach (XmlNode node in nodeList)
						{
							string nodeId = DOMUtil.getNodeJetId(node);
							valueBuffer.Append(nodeId);
							valueBuffer.Append(JetConstant.INNER_SEPARATOR);
							valueBuffer.Append("" + xmlToEdit.MaxLength);
							valueBuffer.Append(JetConstant.ID_SEPARATOR);
						}
					}
				}
			}
			if (valueBuffer.Length > 0)
			{
				valueBuffer.Remove(valueBuffer.Length - 1, 1);
			}
			form.MaxLengthValue = valueBuffer.ToString();
		}
	}
}